package com.project.petLossSystem.util;

import org.springframework.util.StringUtils;

/**
 * result的生成器，就不用改result
 */
public class ResultGenerator {
    private static final String DEFAULT_SUCCESS_MESSAGE = "success";
    private static final String DEFAULT_FAILURE_MESSAGE = "failure";

    private static final int SUCCESS_STATUS_CODE = 200;
    private static final int ERROR_STATUS_CODE = 500;

    // 成功，有data
    public static Result<Object> successResult(Object data){
        return new Result<Object>(SUCCESS_STATUS_CODE, DEFAULT_SUCCESS_MESSAGE, data);
    }

    // 成功，无data
    public static Result successResult(){
        return new Result(SUCCESS_STATUS_CODE, DEFAULT_SUCCESS_MESSAGE);
    }

    // 失败
    public static Result failureResult(String message){
        if(!StringUtils.hasText(message)){
            return new Result(ERROR_STATUS_CODE, DEFAULT_FAILURE_MESSAGE);
        }
        return new Result(ERROR_STATUS_CODE, message);
    }

}
